﻿#------------------------------------------------------------------
#add_library.cmake
#please dont modify this cmake
#2018年6月21日 11点00分
#------------------------------------------------------------------
#add_include
#add_include_bundle
#add_link_library
#add_link_library_bundle
#add_link_library_path

#固定SDK结构！请参照add_sdk.pri

################################################################################
macro(find_framework frameworkname frameworkpath)
    find_library(FRAMEWORK_${frameworkname}
    NAMES ${frameworkname}
    PATHS ${CMAKE_OSX_SYSROOT}/System/Library
    PATH_SUFFIXES Frameworks
    NO_DEFAULT_PATH)
    if( ${FRAMEWORK_${frameworkname}} STREQUAL FRAMEWORK_${frameworkname}-NOTFOUND)
        #MESSAGE(ERROR ": Framework ${frameworkname} not found")
    else()
        set ( ${frameworkpath}  ${FRAMEWORK_${frameworkname}})
        #MESSAGE(STATUS "Framework ${frameworkname} found at ${FRAMEWORK_${frameworkname}}")
    endif()
endmacro(find_framework)

macro(link_framework projectname)
    set(macOSFrameworks Cocoa IOKit DiskArbitration)
    foreach(loop_var ${macOSFrameworks})
        #message(${loop_var})
        find_framework (${loop_var} frameworkpath)
        target_link_libraries(${projectname} ${frameworkpath})
    endforeach(loop_var)
endmacro(link_framework)

macro(link_qt5_libraries projectname)
    set(Qt5Libs Core Widgets Gui Xml Sql Network PrintSupport SerialPort)
    foreach(loop_var ${Qt5Libs})
        #message(Qt5${loop_var})
        find_package(Qt5${loop_var})
        target_link_libraries(${projectname} Qt5::${loop_var})
    endforeach(loop_var)
endmacro(link_qt5_libraries)

#todo: need test
macro(link_qt4_libraries projectname)
    set(Qt4Libs Core Widgets Gui Xml Sql Network PrintSupport SerialPort)
    foreach(loop_var ${Qt4Libs})
        #message(Qt4${loop_var})
        find_package(Qt4${loop_var})
        target_link_libraries(${projectname} Qt4::${loop_var})
    endforeach(loop_var)
endmacro(link_qt4_libraries)

macro (link_qt_libraries projectname)
    if(QT5)
        link_qt5_libraries(${projectname})
    elseif(QT4)
        link_qt4_libraries(${projectname})
    endif()
endmacro(link_qt_libraries)

#LIBGROUPNAME [LIBNAME [LIBINCSUBDIR ]
FUNCTION (ADD_INCLUDE LIBGROUPNAME)
    SET(LIBNAME ${ARGV1})
    SET(LIBINCSUBDIR ${ARGV2})
    IF(NOT LIBNAME)
        SET(LIBNAME ${LIBGROUPNAME})
    ENDIF()

    SET(CUR_INC_PWD ${LIB_SDK_ROOT}/${LIBGROUPNAME}/${CSYS_STD_DIR}/include/${LIBNAME})
    IF(LIBINCSUBDIR)
        SET(CUR_INC_PWD ${CUR_INC_PWD}/${LIBINCSUBDIR})
    ENDIF()
    IF(WIN32)
        #FAIL
        #FILE(TO_NATIVE_PATH ${CUR_INC_PWD} ${CUR_INC_PWD})
        #SUCCESS
        STRING(REPLACE "/" "\\" CUR_INC_PWD ${CUR_INC_PWD})
    ENDIF()

    INCLUDE_DIRECTORIES(${CUR_INC_PWD})
    MESSAGE("Include ${CUR_INC_PWD}")
ENDFUNCTION()

#LIBGROUPNAME [LIBNAME [LIBINCSUBDIR ]
FUNCTION (ADD_INCLUDE_BUNDLE TARGET LIBGROUPNAME)
    SET(LIBNAME ${ARGV1})
    SET(LIBINCSUBDIR ${ARGV2})
    IF(NOT LIBNAME)
        SET(LIBNAME ${LIBGROUPNAME})
    ENDIF()

    IF(APPLE)
        SET(CUR_INC_PWD ${LIB_SDK_ROOT}/${LIBGROUPNAME}/${CSYS_STD_DIR}/lib/${LIBNAME}.framework/Headers)
        IF(LIBINCSUBDIR)
            SET(CUR_INC_PWD ${CUR_INC_PWD}/${LIBINCSUBDIR})
        ENDIF()
    ELSE()
        SET(CUR_INC_PWD ${LIB_SDK_ROOT}/${LIBGROUPNAME}/${CSYS_STD_DIR}/include/${LIBNAME})
        IF(LIBINCSUBDIR)
            SET(CUR_INC_PWD ${CUR_INC_PWD}/${LIBINCSUBDIR})
        ENDIF()
        IF(WIN32)
            #FAIL
            #FILE(TO_NATIVE_PATH ${CUR_INC_PWD} ${CUR_INC_PWD})
            #SUCCESS
            STRING(REPLACE "/" "\\" CUR_INC_PWD ${CUR_INC_PWD})
        ENDIF()
    ENDIF()

    INCLUDE_DIRECTORIES(${CUR_INC_PWD})
    MESSAGE("Include ${CUR_INC_PWD}")
ENDFUNCTION()

#为TARGET从LIBGROUPNAME下链接LIBNAME。
#默认LIBNAME=LIBGROUPNAME。
#CMake的CMakeLists.txt支持多个目标。。。所以不得已必须输入TARGET。
#TARGET LIBGROUPNAME [LIBNAME [LIBREALNAME ]
FUNCTION (ADD_LINK_LIBRARY TARGET LIBGROUPNAME)
    SET(LIBNAME ${ARGV1})
    SET(LIBREALNAME ${ARGV2})
    IF(NOT LIBNAME)
        SET(LIBNAME ${LIBGROUPNAME})
    ENDIF()
    IF(NOT LIBREALNAME)
        ADD_DECORATE_TARGET_NAME(${LIBNAME} LIBREALNAME)
    ENDIF()

    SET(CUR_LIB_PWD ${LIB_SDK_ROOT}/${LIBGROUPNAME}/${CSYS_STD_DIR}/lib)
    IF(WIN32)
        #FAIL
        #FILE(TO_NATIVE_PATH ${CUR_INC_PWD} ${CUR_INC_PWD})
        #SUCCESS
        STRING(REPLACE "/" "\\" CUR_LIB_PWD ${CUR_LIB_PWD})
    ENDIF()

    LINK_DIRECTORIES(${CUR_LIB_PWD})
    TARGET_LINK_LIBRARIES(${TARGET} ${LIBREALNAME})

    MESSAGE("Link ${LIBREALNAME} for ${TARGET} from ${CUR_LIB_PWD}")
ENDFUNCTION()

#TARGET LIBGROUPNAME [LIBNAME [LIBREALNAME ]
FUNCTION (ADD_LINK_LIBRARY_BUNDLE TARGET LIBGROUPNAME)
    SET(LIBNAME ${ARGV1})
    SET(LIBREALNAME ${ARGV2})
    IF(NOT LIBNAME)
        SET(LIBNAME ${LIBGROUPNAME})
    ENDIF()
    IF(NOT LIBREALNAME)
        ADD_DECORATE_TARGET_NAME(${LIBNAME} LIBREALNAME)
    ENDIF()

    SET(CUR_LIB_PWD ${LIB_SDK_ROOT}/${LIBGROUPNAME}/${CSYS_STD_DIR}/lib)
    IF(WIN32)
        #FAIL
        #FILE(TO_NATIVE_PATH ${CUR_INC_PWD} ${CUR_INC_PWD})
        #SUCCESS
        STRING(REPLACE "/" "\\" CUR_LIB_PWD ${CUR_LIB_PWD})
    ENDIF()

    LINK_DIRECTORIES(${CUR_LIB_PWD})
    IF(APPLE)
        TARGET_LINK_LIBRARIES(${TARGET} -framework${LIBNAME})
    ELSE()
        TARGET_LINK_LIBRARIES(${TARGET} ${LIBREALNAME})
    ENDIF()

    MESSAGE("Link ${LIBREALNAME} for ${TARGET} from ${CUR_LIB_PWD}")
ENDFUNCTION()

#LIBGROUPNAME
FUNCTION (ADD_LINK_LIBRARY_PATH LIBGROUPNAME)
    SET(CUR_LIB_PWD ${LIB_SDK_ROOT}/${LIBGROUPNAME}/${CSYS_STD_DIR}/lib)
    IF(WIN32)
        #FAIL
        #FILE(TO_NATIVE_PATH ${CUR_INC_PWD} ${CUR_INC_PWD})
        #SUCCESS
        STRING(REPLACE "/" "\\" CUR_LIB_PWD ${CUR_LIB_PWD})
    ENDIF()

    LINK_DIRECTORIES(${CUR_LIB_PWD})
    MESSAGE("Link from ${CUR_LIB_PWD}")
ENDFUNCTION()
